package com.rw.bootdemo.controller;

import com.rw.bootdemo.cache.RedisCache;
import com.rw.bootdemo.config.MQProducer;
import com.rw.bootdemo.pojo.User;
import com.rw.bootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author rabit
 */
@RestController
public class UserController {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private UserService userService;

    @Autowired
    private MQProducer mqProducer;

    @RequestMapping("users")
    public List<User> getAll(){
      //  int i = 1/0;
        int i = 6;
       List<User> allUsers = userService.getAllUsers();
//       if(i < 10){
//           throw  new NullPointerException("空指针异常");
//       }
       return allUsers;
    }

    @ExceptionHandler(ArithmeticException.class)
    public Map<String, String> getException(){
        Map<String, String> result = new HashMap<>();
        result.put("msg","ArithmeticException报错了");
        return result;
    }

    @RequestMapping("add")
    public Map<String, Object> addRedis(String key,String name){
        Map<String, Object> result = new HashMap<>();
        final boolean flag = redisCache.set(key, name);
        if (flag){
            result.put("msg","设置成功");

        }  else {
            result.put("msg","设置失败");
        }
        return result;

    }
    @RequestMapping("get")
    public Map<String, Object> getRedis(String key){
        Map<String, Object> result = new HashMap<>();
        String s = String.valueOf(redisCache.get(key));
        result.put("msg","查询成功");
        result.put("data",s);
        return result;
    }

    @RequestMapping("addObj")
    public Map<String, Object> addRedisForObj(String key,User user) {
        Map<String, Object> result = new HashMap<>();
        final boolean flag = redisCache.set(key, user);
        if (flag) {
            result.put("msg", "设置成功");
        } else {
            result.put("msg", "设置失败");
        }
        return result;
    }
        @RequestMapping("getObj")
        public Map<String, Object> getRedisObj(String key){
            Map<String, Object> result = new HashMap<>();
          LinkedHashMap res = (LinkedHashMap) redisCache.get(key);
            result.put("msg","查询成功");
            result.put("data",res);
            result.put("name",res.get("uname"));
            return result;
        }
    @RequestMapping("sendMsg")
    public Map<String, Object> getSendMsg(String key){
        Map<String, Object> result = new HashMap<>();
        //service方法的保存数据
        mqProducer.send("test.a.java",key);
        mqProducer.send("tt.java",key+"---"+"tt.java");
        result.put("msg","发送成功");
        return result;
    }


}
